﻿using AmazonMODEL;
using MarketplaceWebServiceOrders;
using MarketplaceWebServiceProducts;
using MODEL;
using MWSRecommendationsSectionService;
using Newtonsoft.Json;
using SysCommon;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace SysController
{
    //  feilongpu   I3D84DTYCppfCy
    // https://svn.yaheecloud.com:8100/svn/Yahee/IT/Projects/Yahee.AmazonListing/Yahee.AmazonListing.API/trunk
    public static partial class HelperController
    {
        #region  3.0 发布类型消息 +  static List<AmazonEnvelopeMessage> AmazonEnvelopeMessageList<T>(List<string> list)
        /// <summary>
        ///  3.0 发布类型消息
        /// </summary>
        /// <typeparam name="T">Product,Price,Image...发布类型</typeparam>
        /// <param name="list">发布类型数据集</param> 
        /// <returns>发布类型消息集</returns>
        public static List<AmazonEnvelopeMessage> AmazonEnvelopeMessageList<T>(List<string> list)
            where T : class
        {
            List<AmazonEnvelopeMessage> msg = new List<AmazonEnvelopeMessage>();
            for (int i = 0; i < list.Count; i++)
            {
                AmazonEnvelopeMessage message = new AmazonEnvelopeMessage()
                {
                    OperationTypeSpecified = true,
                    MessageID = (i + 1).ToString(),
                    OperationType = AmazonEnvelopeMessageOperationType.Update,
                    Item = XmlHelper.ObjectToXmlDESerializer<T>(list[i])
                };
                msg.Add(message);
            }
            return msg;
        }
        #endregion

        #region 4.0 发布类型数据包 + static AmazonEnvelope AmazonEnvelopeObject(string Merchant, List<AmazonEnvelopeMessage> msgList, AmazonEnvelopeMessageType type)
        /// <summary>
        /// 4.0 发布类型数据包
        /// </summary>
        /// <param name="Merchant">卖家编号</param>
        /// <param name="msgList">发布类型消息对象集</param>
        /// <param name="type">发布类型</param>
        /// <returns>发布类型数据包</returns> 
        public static AmazonEnvelope AmazonEnvelopeObject(string merchantCode, List<AmazonEnvelopeMessage> msgList, AmazonEnvelopeMessageType type)
        {
            if (msgList == null || msgList.Count < 1)
                return null;
            AmazonEnvelope envelope = new AmazonEnvelope()
            {
                Header = new Header()
                {
                    DocumentVersion = "1.01",
                    MerchantIdentifier = merchantCode
                },
                MessageType = type,
                Message = msgList.ToArray()
            };
            return envelope;
        }
        #endregion

        #region 5.0 Feeds 服务注册 +  static MarketplaceWebService.MarketplaceWebServiceClient RegisterFeedsService(AppModel appkey)
        /// <summary>
        /// 5.0 Feeds 服务注册
        /// </summary>
        /// <param name="appkey"></param>
        /// <returns></returns>
        public static MarketplaceWebService.MarketplaceWebServiceClient RegisterFeedsService(AppKey appkey)
        {
            MarketplaceWebService.MarketplaceWebServiceClient Client = new MarketplaceWebService.MarketplaceWebServiceClient(
                appkey.AccessKey, appkey.AccessSecret, appkey.AppName, appkey.AppVersion,
            new MarketplaceWebService.MarketplaceWebServiceConfig()
            {
                ServiceURL = appkey.MarketplaceWebServiceURL,
                MaxErrorRetry = 1,
                RequestTimeout = 2000000
            });
            return Client;
        }
        #endregion

        #region 6.0 RegisterRecommendations 服务注册 + static MWSRecommendationsSectionServiceClient RegisterRecommendationsService(AppKey appkey)
        /// <summary>
        /// 6.0 RegisterRecommendations 服务注册
        /// </summary>
        /// <param name="appkey"></param>
        /// <returns></returns>
        public static MWSRecommendationsSectionServiceClient RegisterRecommendationsService(AppKey appkey)
        {
            MWSRecommendationsSectionServiceClient Client = new MWSRecommendationsSectionServiceClient(
                appkey.AccessKey, appkey.AccessSecret, appkey.AppName, appkey.AppVersion,
                new MWSRecommendationsSectionServiceConfig()
                {
                    ServiceURL = appkey.MarketplaceWebServiceURL,
                    MaxErrorRetry = 1
                });
            return Client;
        }
        #endregion

        #region 7.0 Orders 服务注册 +  static MarketplaceWebServiceOrdersClient RegisterOrdersService(AppKey appkey)
        /// <summary>
        /// 7.0 Orders 服务注册
        /// </summary>
        /// <param name="appkey"></param>
        /// <returns></returns>
        public static MarketplaceWebServiceOrdersClient RegisterOrdersService(AppKey appkey)
        {
            MarketplaceWebServiceOrdersClient Client = new MarketplaceWebServiceOrdersClient(appkey.AppName, appkey.AppVersion, appkey.AccessKey, appkey.AccessSecret,
                new MarketplaceWebServiceOrdersConfig()
                {
                    ServiceURL = appkey.MarketplaceWebServiceOrdersURL,
                    MaxErrorRetry = 1
                });
            return Client;
        }
        #endregion

        #region 6.0 Products 服务注册 + static MarketplaceWebServiceProducts.MarketplaceWebServiceProductsClient RegisterProductsService(AppModel appkey)
        /// <summary>
        /// 6.0 Products 服务注册
        /// </summary>
        /// <param name="appkey"></param>
        /// <returns></returns>
        public static MarketplaceWebServiceProducts.MarketplaceWebServiceProductsClient RegisterProductsService(AppKey appkey)
        {
            MarketplaceWebServiceProductsClient client = new MarketplaceWebServiceProductsClient(appkey.AppName, appkey.AppVersion,
                appkey.AccessKey, appkey.AccessSecret, new MarketplaceWebServiceProducts.MarketplaceWebServiceProductsConfig()
                {
                    ServiceURL = appkey.MarketplaceWebServiceProductsURL,
                    MaxErrorRetry = 1
                });
            return client;
        }
        #endregion
    }

    public static partial class HelperController
    {
         
    } 
}